package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.AmountChangeInfoPO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.commons.service.UpdateAmountChangeInfoService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <desc>
 *      修改资金明细变动表余额的消费者
 * </desc>
 *
 * @createDate 2018/09/26
 */
@Component
public class UpdateAmountChangeInfoConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(UpdateAmountChangeInfoConsumer.class);

    //物业设备服务层
    @Autowired
    UpdateAmountChangeInfoService updateAmountChangeInfoService;

    //消息队列
    @Autowired
    private MessageSender messageSender;


    /**
     * <desc>
     *      修改资金明细变动表余额的消费者
     * <desc/>
     * @param   message 消息数据
     * @author Qiang.S
     * @createDate 2018/09/26
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_AMOUNT_CHANGE_INFO_MANAGER_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        byte[] body = message.getBody();
        TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),
                TimeTaskDeadMessageQueuePO.class);
        AmountChangeInfoPO amountChangeInfoPO = JSON.parseObject(JSON.toJSONString(JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent()).get("data")), AmountChangeInfoPO.class);
        //AmountChangeInfoPO amountChangeInfoPO = JSONObject.parseObject(JSON.parseObject(new String(body)).getString("data"),
                //AmountChangeInfoPO.class);
        try {
            updateAmountChangeInfoService.updateAmountChangeInfo(amountChangeInfoPO);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_更新资金明细变动表金额信息业务消费端】:消费异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}